﻿using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.Task.Template;

namespace org.se.fs.presenter.Task.Context
{
    public class Branch : Crud<Domain.Branch>, ITask.IBranch
    {
        //public event FormEvent.FormEventHandler OnGetTotalLateByEmployeeId;
        //public event FormEvent.FormEventHandler OnGetTotalUnderTimeByEmployeeId;

        //protected void TriggerGetTotalLateByEmployeeId(object source, FormEvent e)
        //{
        //    if (OnGetTotalLateByEmployeeId != null)
        //        OnGetTotalLateByEmployeeId(source, e);
        //}

        //protected void TriggerGetTotalUnderTimeByEmployeeId(object source, FormEvent e)
        //{
        //    if (OnGetTotalLateByEmployeeId != null)
        //        OnGetTotalLateByEmployeeId(source, e);
        //}

        //public void GetTotalLateByEmployeeId(Domain.Branch data)
        //{
        //    using (var a = new BackgroundWorker())
        //    {
        //        a.DoWork += BgwGetTotalLateByEmployeeIdDoWork;
        //        a.RunWorkerCompleted += BgwGetTotalLateByEmployeeIdWorkerCompleted;
        //        a.RunWorkerAsync(data);
        //    }
        //}

        //public void GetEmployeeIfDupe(Domain.Branch data)
        //{
        //    using (var a = new BackgroundWorker())
        //    {
        //        a.DoWork += BgwGetTotalUnderTimeByEmployeeIdDoWork;
        //        a.RunWorkerCompleted += BgwGetTotalUnderTimeByEmployeeIdWorkerCompleted;
        //        a.RunWorkerAsync(data);
        //    }
        //}

        #region Protected Backgroud Worker Function
        //protected void BgwGetTotalLateByEmployeeIdDoWork(object sender, DoWorkEventArgs e)
        //{
        //    var data = (Domain.Branch)e.Argument;
        //    //e.Result = new HRMO.Model.Table.Branch().GetTotalLateByEmployeeId(data.Employee.Id);
        //}

        //protected void BgwGetTotalLateByEmployeeIdWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        //{
        //    OnGetTotalLateByEmployeeId(this, new FormEvent(true) { Value = e.Result });
        //}

        //protected void BgwGetTotalUnderTimeByEmployeeIdDoWork(object sender, DoWorkEventArgs e)
        //{
        //    //var data = (Domain.Branch)e.Argument;
        //    //e.Result = new HRMO.Model.Table.Branch().GetTotalUnderTimeByEmployeeId(data.Employee.Id);
        //}

        //protected void BgwGetTotalUnderTimeByEmployeeIdWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        //{
        //    OnGetTotalUnderTimeByEmployeeId(this, new FormEvent(true) { Value = e.Result });
        //}
        #endregion

        #region Protected Overridable Function
        protected override void BgwFetchDoWork(object sender, DoWorkEventArgs e)
        {                                                                                                                                                                                                                                                                                                                                                                                                   
        }

        protected override void BgFetchRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
        }

        protected override void BgwFetchListsDoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = new model.Table.Branch().GetBranchLists();
        }

        protected override void BgFetchListsRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var a = (DataTable)e.Result;
            var data =
                (a.Rows.Cast<DataRow>().Select(
                    dr =>
                    new Domain.Branch
                        {
                            Id = Convert.ToUInt64(dr["id"].ToString()),
                            Name = dr["name"].ToString(),
                            Code = dr["code"].ToString(),
                            Address = dr["address"].ToString(),
                            Email = dr["email"].ToString(),
                            FaxNum = dr["fax_num"].ToString(),
                            MobileNum = dr["mobile_num"].ToString(),
                            TelNum = dr["tel_num"].ToString(),
                            User = new Domain.User
                                       {
                                           Id = Convert.ToUInt64(dr["user_id"].ToString()),
                                           FirstName = dr["first_name"].ToString(),
                                           LastName = dr["last_name"].ToString(),
                                           MidName = dr["middle_name"].ToString(),
                                           Username = dr["user_name"].ToString(),
                                       },
                            CreatedDate = Convert.ToDateTime(dr["created_date"].ToString()),
                        })).ToList();
            TriggerFetchLists(this, new FormEvent(!data.Count.Equals(0)) { Value = data });
        }

        protected override void BgwInsertDoWork(object sender, DoWorkEventArgs e)
        {
            var a = (Domain.Branch)e.Argument;
            a.Id = new model.Table.Branch().Insert(a.Name,a.Code,a.Address,a.Email,a.FaxNum,a.MobileNum,a.TelNum,a.User.Id);
            e.Result = a;
        }

        protected override void BgInsertRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var a = (Domain.Branch)e.Result;
            TriggerInsert(this,
                          new FormEvent(!a.Id.Equals(0))
                          {
                              Value = a,
                              Exception = new Exception(a.Id.Equals(0) ? "Insert failed." : "Insert success.")
                          });
        }

        protected override void BgwUpdateDoWork(object sender, DoWorkEventArgs e)
        {
            var a = (Domain.Branch)e.Argument; 
            IsSuccess = new model.Table.Branch().Update(a.Id, a.Name, a.Code, a.Address, a.Email, a.FaxNum, a.MobileNum, a.TelNum, a.User.Id);
            e.Result = a;
        }

        protected override void BgUpdateRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            TriggerUpdate(this,
                          new FormEvent(IsSuccess)
                          {
                              Value = e.Result,
                              Exception = new Exception(!IsSuccess ? "Update failed." : "Update success.")
                          });
        }

        protected override void BgwDeleteDoWork(object sender, DoWorkEventArgs e)
        {
            var a = (Domain.Branch)e.Argument;
            IsSuccess = new model.Table.Branch().Delete(a.Id);
            e.Result = a;
        }

        protected override void BgDeleteRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            TriggerDelete(this,
                          new FormEvent(IsSuccess)
                          {
                              Value = e.Result,
                              Exception = new Exception(!IsSuccess ? "Update failed." : "Update success.")
                          });
        }

        protected override void BgwToggleDoWork(object sender, DoWorkEventArgs e)
        {
            //var a = (Domain.Branch)e.Argument;
            //a.Active = !a.Active;
            //IsSuccess = new Employee().Toggle(a.Id, a.Active);
            //e.Result = a;
        }

        protected override void BgToggleRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //TriggerToggle(this,
            //              new FormEvent(IsSuccess)
            //              {
            //                  Value = e.Result,
            //                  Exception = new Exception(!IsSuccess ? "Toggle failed." : "Toggle success.")
            //              });
        }
        #endregion
    }
}
